package com.kwok.rbac.application.controller;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.kwok.rbac.application.common.utils.JWTUtil;
import com.kwok.rbac.application.common.utils.R;
import com.kwok.rbac.application.common.utils.UUIDUtil;
import com.kwok.rbac.application.entity.SystemUserToken;
import com.kwok.rbac.application.service.SystemUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class LoginController {

    @Autowired
    private JWTUtil jwtUtil;

    @Autowired
    private SystemUserService systemUserService;


    @PostMapping("/login")
    public R login(@RequestBody Map<String,Object> params){

        //1.获取用户输入的用户名和密码
        String uname = (String) params.get("username");
        String pwd = (String) params.get("password");

        //登录认证
        SystemUserToken systemUserToken = systemUserService.login(uname, pwd);

        //把systemUserToken揉入到JWT的token中
        String jwtToken = jwtUtil.generateToken(systemUserToken);

        //响应给客户端，使用Map将generateToken封装成对象
        Map<String,String> result = new HashMap<>();
        result.put("token",jwtToken);
        return R.success(result);
    }

}
